﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SD.TeachSystem.API.Service;
using SD.TeachSystem.Common;
using SD.TeachSystem.EntityFrameworkCore.Model;
using SD.TeachSystem.IService;
using SD.TeachSystem.Service;

namespace SD.TeachSystem.API.Controller
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DemoController : ControllerBase
    {
        private readonly AllLazyService _allLazyService; 
        public DemoController(AllLazyService allLazyService) 
        {
            _allLazyService = allLazyService;
        }
        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="demo"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<bool> AddDemo(Demo demo)
        {
            if (demo == null)
            {
                return BadRequest("Invalid demo data.");
            }

            var result = _allLazyService.demoService.AddDemoInfo(demo);
            if (result)
            {
                return Ok(true);
            }
            return StatusCode(500, "An error occurred while adding the demo.");
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<bool> DeleteDemo(Guid id)
        {
            var result = _allLazyService.demoService.DelDemoInfo(id);
            if (result)
            {
                return Ok(true);
            }
            return NotFound($"Demo with ID {id} not found.");
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="demo"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<bool> UpdateDemo(Demo demo)
        {
            if (demo == null || demo.Id == Guid.Empty)
            {
                return BadRequest("Invalid demo data.");
            }

            var result = _allLazyService.demoService.UpdateDemoInfo(demo);
            if (result)
            {
                return Ok(true);
            }
            return NotFound($"Demo with ID {demo.Id} not found.");
        }

        /// <summary>
        /// 分页查询数据
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult<List<Demo>> GetDemos(int pageIndex = 1, int pageSize = 10)
        {
            var demos = _allLazyService.demoService.QueryDemoList(pageIndex, pageSize);
            return Ok(demos);
        }

        /// <summary>
        /// 测试导出是否支持Result风格格式
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportTemplate()
        { 
            // 固定的文件路径（假设已知文件路径）
            var rootDirectory = Directory.GetCurrentDirectory();
            var folderPath = Path.Combine(rootDirectory, "Template\\Excel");

            // 假设你要导出的文件名是 'Test.xlsx'
            var fileName = "Test.xlsx";
            var filePath = Path.Combine(folderPath, fileName);

            // 检查文件是否存在
            if (!System.IO.File.Exists(filePath))
            {
                return NotFound("没有找到对应的文件");
            }

            // 读取文件内容
            var fileBytes = System.IO.File.ReadAllBytes(filePath);

            // 返回文件内容，设置文件类型为 Excel 格式
            return File(fileBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
        }
    }
}
